<?php

class Uploader {
    
    public $allowExt = array('jpg' , 'jpeg' , 'png');
    public $maxSize = 300000;
    public $thumb = false;
    public $dstPath = '';
    public $thumbConfig;
    private $pathinfo;
    
    private $_error = array();
    /**
     * @param $dst_path 以/开头和结尾
     */
    public function __construct ($dst_path = '') {
        $this->dstPath = ($dst_path ? $dst_path : '/public/upload/') . $this->getDir();
    
    }
    
    public function upload () {
        
        $files = $this->dealFiles($_FILES);
        
        foreach ($files as $file) {
            if ($file['error'] !== 0){
                continue;
            }
            
            $this->pathinfo = pathinfo($file['name']);
            if (! $this->checkExt()) {
                continue;
            }
            
            if (! $this->checkSize($file['tmp_name'])) {
                continue;
            }
            
            if (! is_uploaded_file($file['tmp_name'])) {
                continue;
            }
            
            $dst_file = $this->dstPath . $this->genRandomName();
            if (! is_dir(ROOT_PATH . $this->dstPath)) {
                mkdir(ROOT_PATH . $this->dstPath, 0777, true);
            }
            
            if (! move_uploaded_file($file['tmp_name'], ROOT_PATH . $dst_file)) {
                continue;
            }
            
            if ($this->thumb){
                $image = new Image();
                $image->makeThumbs(ROOT_PATH . $dst_file, $this->thumbConfig);
            }
            
            $ret[] = array(
                'file_ext' => strtolower($this->pathinfo['extension']) , 
                'file_type' => $file['type'],
                'file_size' => $file['size'],
                'file_name' => $file['name'],
                'target' => $dst_file);
        }
        
        return $ret;
    }
    
    public function dealFiles ($files) {
        $fileArray = array();
        foreach ($files as $file) {
            if (is_array($file['name'])) {
                $keys = array_keys($file);
                $count = count($file['name']);
                for ($i = 0; $i < $count; $i ++) {
                    foreach ($keys as $key) {
                        $fileArray[$i][$key] = $file[$key][$i];
                    }
                }
            } else {
                $fileArray = $files;
            }
            break;
        }
        return $fileArray;
    }
    
    private function checkExt () {
        
        return in_array(strtolower($this->pathinfo['extension']), $this->allowExt);
    }
    
    private function checkSize ($tmpfile) {
        
        return filesize($tmpfile) <= $this->maxSize;
    }
    
    public function getError () {
        return $this->_error;
    }
    
    public function genRandomName () {
        return uniqid(null, true) . '.' . strtolower($this->pathinfo['extension']);
    }
    
    private function getDir () {
        $date = getdate();
        return $date['year'] . '/' . $date['mon'] . '/' . $date['mday'] . '/';
    }

}
?>